<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>单向链表</title>
</head>
<body>
  <script>
    function Node(element) {
      this.element = element;
      this.next = null;
    }

    function LList() {
      this.head = new Node('head');
      this.find = find;
      this.insert = insert;
      this.display = display;
      this.findPrevious = findPrevious;
      this.remove = remove;
    }

    function find(item) {
      let currNode = this.head;
      while (currNode.element !== item){
        currNode = currNode.next;
      }
      return currNode;
    }
    
    function insert(newElement,item) {
      let newNode = new Node(newElement);
      let currNode = this.find(item);
      newNode.next = currNode.next;
      currNode.next = newNode;
    }

    function display() {
      let currNode = this.head;
      while (currNode.next !== null){
        console.log(currNode.next.element);
        currNode = currNode.next;
      }
    }

    function findPrevious(item){
      let currNode = this.head;
      while (currNode.next !== null && currNode.next.element !== item){
        currNode = currNode.next;
      }
      return currNode;
    }

    function remove(item){
      let preNode = this.findPrevious(item);
      let currNode = this.find(item);
      if(preNode.next !== null){
        preNode.next = currNode.next;
        currNode.next  = null;
      }
    }

    let city = new LList();
    city.insert('first','head');
    city.insert('second','first');
    city.insert('third','second');

    city.display();

    city.remove('second');



  </script>
</body>
</html>